package com.dot.web;

import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.dot.service.DwarfBookService;
import com.dot.vo.DwarfBookVo;
import com.dot.vo.DwarfPageVo;



@RequestMapping(value = "")
@Controller
public class FrontControler {
	
	@Autowired
	private DwarfBookService dwarfBookService;
	
	@RequestMapping("/index")
    public ModelAndView index(HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<DwarfBookVo> books = dwarfBookService.getRootBook();
		ModelAndView mv = new ModelAndView("index");
		mv.addObject("books", books);
        return mv;
    }
	
	@RequestMapping("/test")
    public ModelAndView test(HttpServletRequest request, HttpServletResponse response) throws Exception{
		List<DwarfBookVo> books = dwarfBookService.getAll();
		for(DwarfBookVo book : books){
			System.out.println(book.getName());
		}
		ModelAndView mv = new ModelAndView("index");
		mv.addObject("books", books);
        return mv;
    }
	
	@RequestMapping("/list")
    public ModelAndView list(HttpServletRequest request, HttpServletResponse response,String bCode) throws Exception{
        ModelAndView mv = new ModelAndView("list");
        List<DwarfBookVo> books = dwarfBookService.getBookByParentCode(bCode);
        DwarfBookVo book = dwarfBookService.getBookByCode(bCode);
        mv.addObject("books", books);
        mv.addObject("book", book);
        return mv;
    }
	
	@RequestMapping("/detail")
    public ModelAndView detail(String bCode) throws Exception{
        ModelAndView mv = new ModelAndView("detail");
        DwarfBookVo book = dwarfBookService.getBookByCode(bCode);
        DwarfBookVo pbook = null;
        if(book.getParentCode() != null)
        pbook = dwarfBookService.getBookByCode(book.getParentCode());
        List<DwarfPageVo> pages = dwarfBookService.getPageByBookCode(bCode);
        mv.addObject("pages", pages);
        mv.addObject("book", book);
        mv.addObject("pbook", pbook);
        return mv;
    }
	
}
